﻿/// <reference path="../../../../SIR.Web/scripts/_references.js" />
/// <reference path="../../../../SIR.Web/scripts/jquery-1.9.1.js" />
/// <reference path="../../../../SIR.Web/scripts/angular.js" />
/// <reference path="../../../../SIR.Web/scripts/angular-mocks.js" />
/// <reference path="../../../../SIR.Web/scripts/modal.js" />
/// <reference path="../../../../SIR.Web/scripts/slider.js" />
/// <reference path="../../../../SIR.Web/scripts/timer.js" />

/// <reference path="../../../../SIR.Web/app/app.js" />
/// <reference path="../../../../SIR.Web/app/common/common.js" />
/// <reference path="../../../../SIR.Web.Tests/JavaScript/sir-mock.js" />
/// <reference path="../../../../SIR.Web/app/search/search.js" />
/// <reference path="../../../../SIR.Web/app/user/user.js" />
/// <reference path="../../../../SIR.Web/app/authenticate/authenticate.js" />
/// <reference path="../../../../SIR.Web/app/authenticate/services/auth-service.js" />
/// <reference path="../../../../SIR.Web/app/common/services/data-service.js" />
/// <reference path="../../../../SIR.Web/app/common/services/upload-service.js" />
/// <reference path="../../../../SIR.Web/app/user/services/library-service.js" />
/// <reference path="../../../../SIR.Web/app/user/services/library-mediator.js" />
/// <reference path="../../../../SIR.Web/app/user/controllers/user-controller.js" />
/// <reference path="../../../../SIR.Web/app/user/controllers/user-image-upload-controller.js" />
/// <reference path="../../../../SIR.Web/app/user/controllers/user-library-controller.js" />
/// <reference path="../../../../SIR.Web/app/user/controllers/user-library-folder-controller.js" />
/// <reference path="../../../../SIR.Web/app/user/controllers/user-library-image-container-controller.js" />
/// <reference path="../../../../SIR.Web/app/user/controllers/user-view-images-controller.js" />
/// <reference path="../../../../SIR.Web/app/user/controllers/user-view-favorites-controller.js" />

describe('User Control Panel Controller - Log In:', function () {
	'use strict';
	// initialize variables and modules
	var scope, searchController,
		userController, libraryController,
		imageContainerController, viewImagesController,
		folderController,
    	mockLibraryService, mockAuthService,
		mockState, mockDataService, notifyService, mockLibraryMediator;

	beforeEach(module('ui.router'));
	beforeEach(module('ui.slider'));
	beforeEach(module('app.common'));
	beforeEach(module('app.user'));
	beforeEach(module('app.authenticate'));

	beforeEach(inject(function ($controller, $rootScope, notify) {
		// set notify service
		notifyService = notify;
		// create mock data serivce
		mockDataService = {
			download: function () {
				return {
					then: function (callback) {
						callback({ data: { Successful: true, Content: [{}] } });
					}
				};
			}
		};
		// create mock library service
		mockLibraryService = {
			getFolders: function () {
				return {
					then: function (callback) {
						// return 1 folder
						callback({ data: { Successful: true, Content: [{Id:-1}] } });
					}
				}
			},
			getImagesInFolder: function () {
				return {
					then: function (callback) {
						var images = [];
						for (var ii = 0; ii < 10; ii++) {
							images.push({ Id: -1 });
						}
						// return 10 images
						callback({ data: { Successful: true, Content: images } });
					}
				}
			}
		};
		// mock library mediator
		mockLibraryMediator = {
			imageContainerController: {},
			uploadController: {},
			folderController: {},
			// sets the image container controller reference
			setImageContainerController: function (controller) {
				this.imageContainerController = controller;
			},
			// sets the upload controller reference
			setUploadController: function (controller) {
				this.uploadController = controller;
			},
			// sets the folder controller reference
			setFolderController: function (controller) {
				this.folderController = controller;
			},
			// submit the rename for a folder through the upload service
			renameFolder: function (folder) {
				mockDataService.upload('Folder', { Id: folder.Id, Name: folder.newName })
					.then(mockLibraryMediator.folderController.handleRenameFolderResponse);
			}
		};
		// mock authentication service
		mockAuthService = {
			authStatus: { isAuthenticated: false, authenticatedUser: null },
			sendAuthRequest: function () {
				return {
					then: function (callback) {
					}
				}
			}
		};
		// mock ui state
		mockState = {
			params: { folderId: 1 },
			transitionTo: function (name) { this.name = name}
		};
		// new scope
		scope = $rootScope.$new();
		// create controllers with mock values
		userController = $controller('UserController', {
			$scope: scope,
			libraryService: mockLibraryService,
			authService: mockAuthService,
			$state: mockState,
			$stateParams: {},
			dataService: mockDataService
		});

		libraryController = $controller('UserLibraryController', {
			$scope: scope,
			dataService: mockDataService,
			libraryMediator: mockLibraryMediator,
		});

		imageContainerController = $controller('UserLibraryImageContainerController', {
			$scope: scope,
			//libraryMediator: libraryMediator,
			dataService: mockDataService,
			isMobile: false,
			$modal: {}
		});

		viewImagesController = $controller('UserViewImagesController', {
			$scope: scope,
			libraryService: mockLibraryService,
			dataService: mockDataService
		});

		folderController = $controller('UserLibraryFolderController', {
			$scope: scope,
			libraryService: mockLibraryService,
			$state: mockState,
			dataService: mockDataService,
			libraryMediator: mockLibraryMediator
		});
		// spy on notification services
		spyOn(notifyService, 'success').andCallThrough();
		spyOn(notifyService, 'info').andCallThrough();
		spyOn(notifyService, 'warning').andCallThrough();
		spyOn(notifyService, 'error').andCallThrough();
	}));

	describe('Controller - User View Library (Total: 3)', function () {
		it('[1] Navigate back to the splash screen when an unauthenticated user tries to view a library', function () {
			// verify that unauthenticated load pushes to the home page
			expect(mockState.name).toBe('search');
		});

		it('[2] Produce \'User library\' images', function () {
			// verify the images were loaded
			var folders = [{ Name: 'Library', Id: 1 }];
			scope.model.user = { Id: -1, Library: { FolderId: -1 } };
			scope.$apply();
			expect(scope.images.items.length).toBe(10);
		});

		it('[3] Produce \'User library\' folders', function () {
			// verify the folders were loaded
			var folders = [{ Name: 'Library', Id: 1 }];
			scope.model.user = { Id: -1, Library: { FolderId: -1 } };
			scope.$apply();
			expect(scope.folderModel.folders.length).toBe(1);
		});

	});
});